package com.ghn.cocknovel.ui.activity.myorder

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.basemodel.base.BaseHeadVMActivity
import com.example.basemodel.base.BaseRecyclerAdapter
import com.example.basemodel.base.DefaultHeadStyle
import com.ghn.cocknovel.R
import com.ghn.cocknovel.databinding.ActivityMyorderBinding
import com.ghn.cocknovel.ui.activity.myorder.vm.MyorderViewModel
import com.kt.NetworkModel.bean.queryDriverTransitOrderListBean
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener


class MyorderActivity :
    BaseHeadVMActivity<MyorderViewModel, ActivityMyorderBinding, DefaultHeadStyle>(),
    OnRefreshLoadMoreListener {
    private var page = 1
    var adapter: BaseRecyclerAdapter<queryDriverTransitOrderListBean>? = null
    override fun layoutId() = R.layout.activity_myorder

    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView(savedInstanceState: Bundle?) {
        mHeadStyle.setTitleColor("我的订单", getColor(R.color.white))
        mHeadStyle.setLeftImageResource(R.mipmap.ic_back_white)
        mHeadStyle.setTitleBackgroundColor(getColor(R.color.blue1))
        mViewModel.queryDriverTransitOrderList(page,intent.getStringExtra("taskId").toString())
        mDatabind.swipeRefreshLayout.setOnRefreshListener(this)
        mDatabind.swipeRefreshLayout.setOnLoadMoreListener(this)
        val querlist = mutableListOf<queryDriverTransitOrderListBean>()
        mDatabind.MyorderRecyclerview.layoutManager = LinearLayoutManager(this)
        adapter = object : BaseRecyclerAdapter<queryDriverTransitOrderListBean>(querlist) {
            override fun bindData(holder: BaseViewHolder?, position: Int) {
                holder?.itemView?.findViewById<TextView>(R.id.item_order_ordernumber)?.text =
                    datas[position].orderId
                holder?.itemView?.findViewById<TextView>(R.id.item_order_ordertime)?.text =
                    datas[position].payTime
                holder?.itemView?.findViewById<TextView>(R.id.item_order_upaddress)?.text =
                    datas[position].originAddress
                holder?.itemView?.findViewById<TextView>(R.id.item_order_offaddress)?.text =
                    datas[position].destinationAddress
                when (datas[position].orderStatus) {
                    1 -> {
                        holder?.itemView?.findViewById<TextView>(R.id.item_order_type)?.text =
                            "待支付"
                    }

                    2 -> {
                        holder?.itemView?.findViewById<TextView>(R.id.item_order_type)?.text =
                            "待呼叫"
                    }

                    3 -> {
                        holder?.itemView?.findViewById<TextView>(R.id.item_order_type)?.text =
                            "呼叫中"
                    }

                    4 -> {
                        holder?.itemView?.findViewById<TextView>(R.id.item_order_type)?.text =
                            "司机已接单"
                    }

                    5 -> {
                        holder?.itemView?.findViewById<TextView>(R.id.item_order_type)?.text =
                            "司机到达"
                    }

                    6 -> {
                        holder?.itemView?.findViewById<TextView>(R.id.item_order_type)?.text =
                            "已上车"
                    }

                    7 -> {
                        holder?.itemView?.findViewById<TextView>(R.id.item_order_type)?.text =
                            "行程结束"
                    }

                    8 -> {
                        holder?.itemView?.findViewById<TextView>(R.id.item_order_type)?.text =
                            "已取消"
                    }

                    9 -> {
                        holder?.itemView?.findViewById<TextView>(R.id.item_order_type)?.text =
                            "已退款"
                    }

                }
                holder?.itemView?.findViewById<ConstraintLayout>(R.id.item_order_const)?.setOnClickListener {

                    val intent = Intent(this@MyorderActivity, MyOrderContentActivity::class.java)
                    intent.putExtra("orderId", datas[position].orderId)
                    startActivity(intent)
                }
            }

            override val layoutId: Int = R.layout.order_rv
        }
        mDatabind.MyorderRecyclerview.adapter = adapter
        mViewModel.queryDriverTransitOrder.observe(this) {
            if (it != null) {
                adapter?.addData(it)
                mDatabind.swipeRefreshLayout.finishLoadMore()
                mDatabind.swipeRefreshLayout.finishRefresh()
            }
        }
    }

    override fun createObserver() {

    }

    /**
     * 刷新
     */
    override fun onRefresh(refreshLayout: RefreshLayout?) {
        page = 1
        mViewModel.queryDriverTransitOrderList(page,intent.getStringExtra("taskId").toString())
    }

    /**
     * 加载更多
     */
    override fun onLoadMore(refreshLayout: RefreshLayout?) {
        page++
        mViewModel.queryDriverTransitOrderList(page,intent.getStringExtra("taskId").toString())
    }

}
